package com.crm.common.util;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 同步es
 * @author: zk
 * @Date: 2021-11-08 10:52
 **/
@Slf4j
public class ESUtil {

    /**
     * 合同的编辑删除
     * @param contractId
     * @param url
     * @return
     */
    public static String syncConctract(String contractId,String url){
        Map<String, Object> map = new HashMap<>();
        map.put("contractId", contractId);
        try {
            return HttpUtil.post(url+"/contract/insertOrUpdate",map);
        } catch (Exception e) {
            log.info(contractId+"合同,同步失败");
        }
        return null;
    }

    /**
     * 合同新增
     * @param map
     * @param url
     */
    public static void syncContractEntity(Map<String, Object> map,String url){
        try {
            HttpUtil.post(url+"/contract/insertOrUpdateEntity",map);
        } catch (Exception e) {
            log.info(map.get("contractId")+"合同,同步失败");
        }
    }


    /**
     * 跟进新增
     * @param map
     * @param url
     */
    public static void syncCpClueFollow(Map<String, Object> map,String url){
        try {
            HttpUtil.post(url+"/cpClueFollow/addCpClueFollow",map);
        } catch (Exception e) {
            log.info(map.get("clueFollowId")+"跟进,同步失败");
        }
    }

    /**
     * 跟进编辑删除
     * @param followId
     * @param url
     * @return
     */
    public static String syncFollow(String followId,String url){
        Map<String, Object> map = new HashMap<>();
        map.put("clueFollowId", followId);
        try {
            return HttpUtil.post(url+"/cpClueFollow/insertOrUpdate",map);
        } catch (Exception e) {
            log.info(followId+"跟进,同步失败");
        }
        return null;
    }

    /**
     * 订单编辑删除
     * @param orderId
     * @param url
     * @return
     */
    public static String syncOrder(String orderId,String url){
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        try {
            return HttpUtil.post(url+"/order/insertOrUpdate",map);
        } catch (Exception e) {
            log.info(orderId+"订单,同步失败");
        }
        return null;
    }

    public static void syncCustomer(Map<String, Object> map, String accountId, String msg, String url) {

        String token = getToken(accountId,msg,url);

        map.put("authorization",token);
        map.put("key",accountId);
        try {
            String result = HttpUtil.post(url+"/cutomer/insertOrUpdate",map);
            log.info(url +result +msg);
        } catch (HttpException e) {
            log.info(msg+"ES同步失败");
        }
    }

    public static String getToken(String accountId, String msg,String url) {
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("accountId", accountId);
        try {
            return HttpUtil.post(url+"/token/initToken",tokenMap);
        } catch (Exception e) {
            log.info(msg+"获取token失败");
        }
        return null;
    }

}
